#include <iostream>

using namespace std;

#define MAX 1000

// 联系人结构体
struct Person {
  string name;  // 姓名
  int sex; // 性别
  int age;  // 年龄
  string phone; // 电话号码
  string address; // 家庭住址
};

// 通讯录结构体
struct Addressbooks {
  struct Person personArray[MAX];
  int m_size; // 通讯录中人员个数
};

/**
 * 生产菜单输出
 */
void showMenu() {
  cout << "***************************" << endl;
  cout << "******  1、添加联系人  ******" << endl;
  cout << "******  2、显示联系人  ******" << endl;
  cout << "******  3、删除联系人  ******" << endl;
  cout << "******  4、查找联系人  ******" << endl;
  cout << "******  5、修改联系人  ******" << endl;
  cout << "******  6、清空联系人  ******" << endl;
  cout << "******  0、退出通讯录  ******" << endl;
  cout << "***************************" << endl;
}

/**
 * 添加联系人
 * @param addressBook 联系人存储数组
 * @param len 数组长度
 */
void addPeople(Addressbooks *abs) {
  //  判断通讯录是否已满
  if (abs->m_size == MAX) {
    cout << "通讯录已满，无法添加！";
  } else {
    int m_Size = abs->m_size;
    cout << "请输入姓名" << endl;
    string name;
    bool isSameName = false;
    do {
      isSameName = false;
      cin >> name;
      for (int i = 0; i < abs->m_size; ++i) {
        if (abs->personArray[i].name == name) {
          cout << "联系人已存在，请重新输入" << endl;
          isSameName = true;
          break;
        }
      }
    } while (isSameName);
    abs->personArray[m_Size].name = name;
    cout << "请输入性别：" << endl;
    cout << "1 --- 男" << endl;
    cout << "2 --- 女" << endl;
    int sex = 0;
    while (true) {
      cin >> sex;
      // 如果输入正确值退出循环，错误提示重新输入
      if (sex == 1 || sex == 2) {
        abs->personArray[m_Size].sex = sex;
        break;
      } else {
        cout << "请输入有误，请重新输入" << endl;
      }
    }
    cout << "请输入年龄：" << endl;
    int age = 0;
    cin >> age;
    abs->personArray[m_Size].age = age;

    cout << "请输入电话号码：" << endl;
    string phone;
    cin >> phone;
    abs->personArray[m_Size].phone = phone;

    cout << "请输入家庭住址：" << endl;
    string address;
    cin >> address;
    abs->personArray[m_Size].address = address;

    cout << "联系人 " << name << " 添加成功！" << endl;
    abs->m_size = ++m_Size;
  }
}

/**
 * 显示所有联系人
 * @param addressBook
 * @param len
 */
void showAddressBook(struct Addressbooks const *abs) {
  if (abs->m_size == 0) {
    cout << "记录为空" << endl;
  } else {
    for (int i = 0; i < abs->m_size; ++i) {
      cout << "姓名：" << abs->personArray[i].name << " 性别：" << (abs->personArray[i].sex == 1 ? "男" : "女") << " 年龄："
           << abs->personArray[i].age << " 电话："
           << abs->personArray[i].phone << " 家庭住址：" << abs->personArray[i].address << endl;
    }
  }
}

/**
 * 检测联系人是否存在，存在返回位置，不存在返回-1
 * @param abs 通讯录
 * @param name 对比姓名
 * @return int 联系人所在数组下标，-1 不存在
 */
int isExist(Addressbooks *abs, string name) {
  for (int i = 0; i < abs->m_size; ++i) {
    if (abs->personArray[i].name == name) {
      return i;
    }
  }
  return -1;
}

/**
 * 查找并删除联系人
 * @param abs
 */
void delPeople(Addressbooks *abs) {
  cout << "请输入需要删除的联系人姓名：" << endl;
  string name;
  cin >> name;
  int index = isExist(abs, name);
  if (index != -1) {
//    通过数据迁移进行删除
    for (int i = index; i < abs->m_size; ++i) {
      abs->personArray[i] = abs->personArray[i + 1];
    }
    abs->m_size--;  // 人数减一
    cout << "删除成功！" << endl;
  } else {
    cout << "未找到姓名为：" << name << " 的联系人!" << endl;
  }
}

/**
 * 查找指定联系人信息
 */
void findPerson(Addressbooks *abs) {
  cout << "请输入您要查找的联系人" << endl;
  string name;
  cin >> name;
//  判断指定的联系人是否存在
  int ret = isExist(abs, name);
  if (ret != -1) {
    cout << "姓名：" << abs->personArray[ret].name << " 性别：" << (abs->personArray[ret].sex == 1 ? "男" : "女") << " 年龄："
         << abs->personArray[ret].age << " 电话："
         << abs->personArray[ret].phone << " 家庭住址：" << abs->personArray[ret].address << endl;
  } else {
    cout << "查无此人" << endl;
  }
}

/**
 * 修改指定联系人
 */
void modifyPerson(Addressbooks *abs) {
  cout << "请输入您要修改的联系人" << endl;
  string name;
  cin >> name;

  int ret = isExist(abs, name);
  if (ret != -1) {
    // 姓名
    string name;
    cout << "请输入姓名" << endl;
    cin >> name;
    abs->personArray[ret].name = name;
    // 性别
    // 年龄
    // 电话
    // 住址
    cout << "请输入性别：" << endl;
    cout << "1 --- 男" << endl;
    cout << "2 --- 女" << endl;
    int sex = 0;
    while (true) {
      cin >> sex;
      // 如果输入正确值退出循环，错误提示重新输入
      if (sex == 1 || sex == 2) {
        abs->personArray[ret].sex = sex;
        break;
      } else {
        cout << "请输入有误，请重新输入" << endl;
      }
    }
    cout << "请输入年龄：" << endl;
    int age = 0;
    cin >> age;
    abs->personArray[ret].age = age;

    cout << "请输入电话号码：" << endl;
    string phone;
    cin >> phone;
    abs->personArray[ret].phone = phone;

    cout << "请输入家庭住址：" << endl;
    string address;
    cin >> address;
    abs->personArray[ret].address = address;

    cout << "联系人 " << name << " 修改成功！" << endl;
  } else {
    cout << "查无此人" << endl;
  }
}

/**
 * 清空所有联系人
 * @return
 */
void cleanPerson(Addressbooks *abs) {
  abs->m_size = 0;  // 将当前记录联系人数量置为0
  cout << "通讯录已清空" << endl;
}

int main() {

  Addressbooks abs;
  abs.m_size = 0;

  int select = 0; // 定义用户输入
  while (true) {
    showMenu();
    cin >> select;

    switch (select) {
      case 0: // 退出通讯录
        return 0;
        break;
      case 1: // 添加联系人
        addPeople(&abs);  // 利用地址传递，可以修改实参
        break;
      case 2: // 显示联系人
        showAddressBook(&abs);
        break;
      case 3: // 删除联系人
        delPeople(&abs);
        break;
      case 4: // 查找联系人
        findPerson(&abs);
        break;
      case 5: // 修改联系人
        modifyPerson(&abs);
        break;
      case 6: // 清空联系人
        cleanPerson(&abs);
        break;
    }
    system("read");
  }
}
